package Day2025_02_25;

public class LeetCode1314 {
    public int[][] matrixBlockSum(int[][] mat, int k) {
        int n = mat.length, m = mat[0].length;
        int[][] dp = new int[n + 1][m + 1];
        int[][] ans = new int[n][m];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int x1 = Math.max(0, i - k - 1), y1 = Math.max(0, j - k - 1);
                int x2 = Math.min(n, i + k), y2 = Math.min(m, j + k);
                ans[i - 1][j - 1] = dp[x2][y2] - dp[x2][y1] -dp[x1][y2] + dp[x1][y1];
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        LeetCode1314 leetCode1314 = new LeetCode1314();
        System.out.println(leetCode1314.matrixBlockSum(new int[][]{{1,2,3},{4,5,6},{7,8,9}}, 1));
    }
}
